package ynu.sxp.demoapp.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.sxp.demoapp.entity.*;
import ynu.sxp.demoapp.repository.*;

import java.math.BigDecimal;
import java.util.UUID;

@Service
public class DatabaseInitializer implements CommandLineRunner {

    @Autowired
    private BusinessRepository businessRepository;

    @Autowired
    private FoodRepository foodRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private CartRepository cartRepository;

    @Autowired
    private DeliveryAddressRepository deliveryAddressRepository;

    @Autowired
    private OrdersRepository ordersRepository;

    @Override
    public void run(String... args) throws Exception {
        initDatabase();
    }

    @Transactional // 添加事务管理
    public void initDatabase() {
        // 初始化商家
        Business business1 = new Business();
        business1.setId(UUID.randomUUID()); // 使用 UUID 类型的 ID
        business1.setBusinessName("万家饺子（软件园E18店）");
        business1.setBusinessAddress("北京市朝阳区");
        business1.setBusinessExplain("各种饺子");
        business1.setBusinessImg("/sj01.png"); // 图片路径
        business1.setOrderTypeId(1);
        business1.setStarPrice(new BigDecimal("20.00"));
        business1.setDeliveryPrice(new BigDecimal("5.00"));
        insertBusinessIfNotExists(business1);

        Business business2 = new Business();
        business2.setId(UUID.randomUUID());
        business2.setBusinessName("小锅饭豆腐馆（全运店）");
        business2.setBusinessAddress("北京市朝阳区");
        business2.setBusinessExplain("各种豆腐");
        business2.setBusinessImg("/sj02.png");
        business2.setOrderTypeId(1);
        business2.setStarPrice(new BigDecimal("20.00"));
        business2.setDeliveryPrice(new BigDecimal("5.00"));
        insertBusinessIfNotExists(business2);

        Business business3 = new Business();
        business3.setId(UUID.randomUUID());
        business3.setBusinessName("麦当劳麦乐送（全运店）");
        business3.setBusinessAddress("北京市朝阳区");
        business3.setBusinessExplain("汉堡和炸鸡");
        business3.setBusinessImg("/sj03.png");
        business3.setOrderTypeId(1);
        business3.setStarPrice(new BigDecimal("20.00"));
        business3.setDeliveryPrice(new BigDecimal("5.00"));
        insertBusinessIfNotExists(business3);

        Business business4 = new Business();
        business4.setId(UUID.randomUUID());
        business4.setBusinessName("米村拌饭（津南店）");
        business4.setBusinessAddress("北京市朝阳区");
        business4.setBusinessExplain("韩式拌饭");
        business4.setBusinessImg("/sj04.png");
        business4.setOrderTypeId(1);
        business4.setStarPrice(new BigDecimal("20.00"));
        business4.setDeliveryPrice(new BigDecimal("5.00"));
        insertBusinessIfNotExists(business4);

        Business business5 = new Business();
        business5.setId(UUID.randomUUID());
        business5.setBusinessName("申记串道（中海康城店）");
        business5.setBusinessAddress("北京市朝阳区");
        business5.setBusinessExplain("烧烤串串");
        business5.setBusinessImg("/sj05.png");
        business5.setOrderTypeId(1);
        business5.setStarPrice(new BigDecimal("20.00"));
        business5.setDeliveryPrice(new BigDecimal("5.00"));
        insertBusinessIfNotExists(business5);

        Business business6 = new Business();
        business6.setId(UUID.randomUUID());
        business6.setBusinessName("半亩良田排骨米饭");
        business6.setBusinessAddress("北京市朝阳区");
        business6.setBusinessExplain("排骨米饭");
        business6.setBusinessImg("/sj06.png");
        business6.setOrderTypeId(1);
        business6.setStarPrice(new BigDecimal("20.00"));
        business6.setDeliveryPrice(new BigDecimal("5.00"));
        insertBusinessIfNotExists(business6);

        Business business7 = new Business();
        business7.setId(UUID.randomUUID());
        business7.setBusinessName("茶兮");
        business7.setBusinessAddress("北京市朝阳区");
        business7.setBusinessExplain("奶茶和饮品");
        business7.setBusinessImg("/sj07.png");
        business7.setOrderTypeId(1);
        business7.setStarPrice(new BigDecimal("20.00"));
        business7.setDeliveryPrice(new BigDecimal("5.00"));
        insertBusinessIfNotExists(business7);

        Business business8 = new Business();
        business8.setId(UUID.randomUUID());
        business8.setBusinessName("唯一水果捞");
        business8.setBusinessAddress("北京市朝阳区");
        business8.setBusinessExplain("水果捞和甜品");
        business8.setBusinessImg("/sj08.png");
        business8.setOrderTypeId(1);
        business8.setStarPrice(new BigDecimal("20.00"));
        business8.setDeliveryPrice(new BigDecimal("5.00"));
        insertBusinessIfNotExists(business8);

        Business business9 = new Business();
        business9.setId(UUID.randomUUID());
        business9.setBusinessName("美食餐厅");
        business9.setBusinessAddress("北京市朝阳区");
        business9.setBusinessExplain("提供各种美食");
        business9.setBusinessImg("/sj09.png");
        business9.setOrderTypeId(1);
        business9.setStarPrice(new BigDecimal("20.00"));
        business9.setDeliveryPrice(new BigDecimal("5.00"));
        insertBusinessIfNotExists(business9);

        // 初始化食品
        Food food1 = new Food();
        food1.setId(UUID.randomUUID()); // 使用 UUID 类型的 ID
        food1.setFoodName("纯肉鲜肉（水饺）");
        food1.setFoodExplain("新鲜猪肉");
        food1.setFoodImg("/sp01.png"); // 图片路径
        food1.setFoodPrice(new BigDecimal("15.00")); // 价格
        food1.setBusinessId(business1.getId().hashCode()); // 关联商家
        insertFoodIfNotExists(food1);

        Food food2 = new Food();
        food2.setId(UUID.randomUUID());
        food2.setFoodName("玉米鲜肉（水饺）");
        food2.setFoodExplain("新鲜猪肉");
        food2.setFoodImg("/sp02.png");
        food2.setFoodPrice(new BigDecimal("14.00"));
        food2.setBusinessId(business1.getId().hashCode()); // 关联商家
        insertFoodIfNotExists(food2);

        Food food3 = new Food();
        food3.setId(UUID.randomUUID());
        food3.setFoodName("虾仁三鲜（蒸饺）");
        food3.setFoodExplain("新鲜虾仁");
        food3.setFoodImg("/sp03.png");
        food3.setFoodPrice(new BigDecimal("18.00"));
        food3.setBusinessId(business1.getId().hashCode()); // 关联商家
        insertFoodIfNotExists(food3);

        Food food4 = new Food();
        food4.setId(UUID.randomUUID());
        food4.setFoodName("素三鲜（蒸饺）");
        food4.setFoodExplain("新鲜蔬菜");
        food4.setFoodImg("/sp04.png");
        food4.setFoodPrice(new BigDecimal("12.00"));
        food4.setBusinessId(business1.getId().hashCode()); // 关联商家
        insertFoodIfNotExists(food4);

        Food food5 = new Food();
        food5.setId(UUID.randomUUID());
        food5.setFoodName("角瓜鸡蛋（水饺）");
        food5.setFoodExplain("新鲜角瓜和鸡蛋");
        food5.setFoodImg("/sp05.png");
        food5.setFoodPrice(new BigDecimal("13.00"));
        food5.setBusinessId(business1.getId().hashCode()); // 关联商家
        insertFoodIfNotExists(food5);

        Food food6 = new Food();
        food6.setId(UUID.randomUUID());
        food6.setFoodName("小白菜肉（水饺）");
        food6.setFoodExplain("新鲜小白菜和猪肉");
        food6.setFoodImg("/sp06.png");
        food6.setFoodPrice(new BigDecimal("16.00"));
        food6.setBusinessId(business1.getId().hashCode()); // 关联商家
        insertFoodIfNotExists(food6);

        Food food7 = new Food();
        food7.setId(UUID.randomUUID());
        food7.setFoodName("芹菜牛肉（水饺）");
        food7.setFoodExplain("新鲜芹菜和牛肉");
        food7.setFoodImg("/sp07.png");
        food7.setFoodPrice(new BigDecimal("20.00"));
        food7.setBusinessId(business1.getId().hashCode()); // 关联商家
        insertFoodIfNotExists(food7);

        Food food8 = new Food();
        food8.setId(UUID.randomUUID());
        food8.setFoodName("虾腰鲜肉（蒸饺）");
        food8.setFoodExplain("新鲜虾仁和猪肉");
        food8.setFoodImg("/sp08.png");
        food8.setFoodPrice(new BigDecimal("19.00"));
        food8.setBusinessId(business1.getId().hashCode()); // 关联商家
        insertFoodIfNotExists(food8);

        Food food9 = new Food();
        food9.setId(UUID.randomUUID());
        food9.setFoodName("青椒鲜肉（水饺）");
        food9.setFoodExplain("新鲜青椒和猪肉");
        food9.setFoodImg("/sp09.png");
        food9.setFoodPrice(new BigDecimal("17.00"));
        food9.setBusinessId(business1.getId().hashCode()); // 关联商家
        insertFoodIfNotExists(food9);

        // 初始化用户
        User user1 = new User();
        user1.setId(UUID.randomUUID()); // 使用 UUID 类型的 ID
        user1.setUserId("user1");
        user1.setPassword("password1");
        user1.setUserName("张三");
        user1.setUserSex(1);
        user1.setUserImg("/yhtx01.png"); // 示例用户头像 URL
        user1.setDelTag(1);
        insertUserIfNotExists(user1);

        // 初始化购物车
        Cart cart1 = new Cart();
        cart1.setId(UUID.randomUUID()); // 使用 UUID 类型的 ID
        cart1.setFoodId(1); // 使用 Integer 类型的值
        cart1.setBusinessId(business1.getId().hashCode()); // 关联商家
        cart1.setUserId(user1.getUserId());
        cart1.setQuantity(2);
        insertCartIfNotExists(cart1);

        // 初始化送货地址
        DeliveryAddress address1 = new DeliveryAddress();
        address1.setId(UUID.randomUUID()); // 使用 UUID 类型的 ID
        address1.setContactName("张三");
        address1.setContactSex(1);
        address1.setContactTel("13800138000");
        address1.setAddress("北京市朝阳区");
        address1.setUserId(user1.getUserId());
        insertDeliveryAddressIfNotExists(address1);

        // 初始化订单
        Orders order1 = new Orders();
        order1.setId(UUID.randomUUID()); // 使用 UUID 类型的 ID
        order1.setUserId(user1.getUserId());
        order1.setBusinessId(business1.getId().hashCode()); // 关联商家
        order1.setOrderDate("2023-10-01");
        order1.setOrderTotal(new BigDecimal("50.00"));
        order1.setDaId(1); // 使用 Integer 类型的值
        order1.setOrderState(0);
        insertOrderIfNotExists(order1);
    }

    private void insertBusinessIfNotExists(Business business) {
        if (businessRepository.findByBusinessName(business.getBusinessName()) == null) {
            businessRepository.save(business);
        }
    }

    private void insertFoodIfNotExists(Food food) {
        if (foodRepository.findByFoodNameAndBusinessId(food.getFoodName(), food.getBusinessId()) == null) {
            foodRepository.save(food);
        }
    }

    private void insertUserIfNotExists(User user) {
        if (userRepository.findByUserId(user.getUserId()) == null) {
            userRepository.save(user);
        }
    }

    private void insertCartIfNotExists(Cart cart) {
        if (cartRepository.findByUserIdAndBusinessIdAndFoodId(cart.getUserId(), cart.getBusinessId(), cart.getFoodId()) == null) {
            cartRepository.save(cart);
        }
    }

    private void insertDeliveryAddressIfNotExists(DeliveryAddress address) {
        if (deliveryAddressRepository.findByUserIdAndAddress(address.getUserId(), address.getAddress()) == null) {
            deliveryAddressRepository.save(address);
        }
    }

    private void insertOrderIfNotExists(Orders order) {
        if (ordersRepository.findByUserIdAndBusinessIdAndOrderDate(order.getUserId(), order.getBusinessId(), order.getOrderDate()) == null) {
            ordersRepository.save(order);
        }
    }
}